package cn.threewaters.qtyy.web.controller.business;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.threewaters.qtyy.common.bean.ResponseJson;
import cn.threewaters.qtyy.dic.one.queryparam.SaveAi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "智能模式")
@Controller
@RequestMapping(value = "/ai")
public class AiController {

	@Resource
	@Qualifier("oneJdbcTemplate")
	private JdbcTemplate oneJdbcTemplate;

	@ApiIgnore
	@ApiOperation(value = "智能模式展示页面")
	@RequestMapping(value = "/main", method = RequestMethod.GET)
	public ModelAndView main(ModelMap modelMap, String sblx, String orgcode, String storehousecode) {
		modelMap.put("sblx", sblx);
		modelMap.put("orgcode", orgcode);
		modelMap.put("storehousecode", storehousecode);

		List<Map<String, Object>> queryForList = oneJdbcTemplate.queryForList(
				"select id,opentemp,closetemp,opentime,closetime,isusetemp,isusetime from la_aiconfig where orgcode = '"
						+ orgcode + "' and storehousecode = '" + storehousecode + "' and type = '" + sblx + "'");
		if (queryForList.size() > 0) {
			modelMap.put("id", queryForList.get(0).get("id"));
			modelMap.put("opentemp", queryForList.get(0).get("opentemp"));
			modelMap.put("closetemp", queryForList.get(0).get("closetemp"));
			modelMap.put("opentime", queryForList.get(0).get("opentime"));
			modelMap.put("closetime", queryForList.get(0).get("closetime"));
			modelMap.put("isusetemp", queryForList.get(0).get("isusetemp"));
			modelMap.put("isusetime", queryForList.get(0).get("isusetime"));
		}
		return new ModelAndView("business/ai/ai", modelMap);
	}

	@ApiOperation(value = "智能模式保存")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public ResponseJson save(SaveAi saveAi) throws Exception {
		if (saveAi.getIsusetime() != null) {
			saveAi.setIsusetime("1");
		}
		if (saveAi.getIsusetemp() != null) {
			saveAi.setIsusetemp("1");
		}
		String sql = "";
		if (saveAi.getId() != null && !saveAi.getId().equals("")) {
			sql = "update la_aiconfig set opentemp = ?,closetemp = ?,opentime = ?,closetime = ?,isusetemp = ?,isusetime = ? where id = ?";
			oneJdbcTemplate.update(sql, saveAi.getOpentemp(), saveAi.getClosetemp(), saveAi.getOpentime(),
					saveAi.getClosetime(), saveAi.getIsusetemp(), saveAi.getIsusetime(), saveAi.getId());
		} else {
			sql = "insert into la_aiconfig(orgcode,storehousecode,type,opentemp,closetemp,opentime,closetime,isusetemp,isusetime) values (?,?,?,?,?,?,?,?,?)";
			oneJdbcTemplate.update(sql, saveAi.getOrgcode(), saveAi.getStorehousecode(), saveAi.getSblx(),
					saveAi.getOpentemp(), saveAi.getClosetemp(), saveAi.getOpentime(), saveAi.getClosetime(),
					saveAi.getIsusetemp(), saveAi.getIsusetime());
		}
		return ResponseJson.builder().success(true).msg("保存成功").obj(null).build();
	}

}
